'******************************************
'Nybble BASIC
'(C) Ben Sherratt 2002
'Created by Ben Sherratt when he was 15
'All rights reserved
'******************************************


$INClUDE "rapidq.inc"

'API calls the program uses
DECLARE SUB FatalAppExit Lib "kernel32" Alias "FatalAppExitA" (Action AS Long, MessageText AS String)
DECLARE FUNCTION ClipCursor Lib "user32" Alias "ClipCursor" (Rect As LONG) As Long
'ClipCursor rect
    DIM WinRect AS QRECT
'===============
DECLARE FUNCTION ShowCursor Lib "user32" Alias "ShowCursor" (blnShow As Long) As Long
DECLARE FUNCTION SwapMouseButton Lib "user32" Alias "SwapMouseButton" (blnSwap As Long) As Long
DECLARE FUNCTION SleepEx LIB "kernel32" ALIAS "SleepEx" (intSleepTime AS INTEGER, blnWaitEnd AS LONG) AS LONG

'BMP resources for the IDE
$RESOURCE NewImg AS "C:\NybbleBASIC\new.bmp"
$RESOURCE OpenImg AS "C:\NybbleBASIC\open.bmp"
$RESOURCE SaveImg AS "C:\NybbleBASIC\save.bmp"
$RESOURCE CutImg AS "C:\NybbleBASIC\cut.bmp"
$RESOURCE CopyImg AS "C:\NybbleBASIC\copy.bmp"
$RESOURCE PasteImg AS "C:\NybbleBASIC\paste.bmp"
$RESOURCE RunImg AS "C:\NybbleBASIC\run.bmp"
$RESOURCE CompileImg AS "C:\NybbleBASIC\compile.bmp"
$RESOURCE HelpImg AS "C:\NybbleBASIC\help.bmp"
$RESOURCE Top AS "C:\NybbleBASIC\Top.bmp"

'BMP resources for the help window
$RESOURCE HelpSideImg AS "C:\NybbleBASIC\helpside.bmp"

'ICO Resource for help window
$RESOURCE HelpFormICOHandle AS "C:\NybbleBASIC\help.ico"

'BMP resources for the output window
$RESOURCE Title AS "C:\NybbleBASIC\Titlebar.BMP"
$RESOURCE CloseImg AS "C:\NybbleBASIC\close.BMP"
$RESOURCE MinImg AS "C:\NybbleBASIC\Minimize.BMP"
$RESOURCE CloseImg2 AS "C:\NybbleBASIC\close2.BMP"
$RESOURCE MinImg2 AS "C:\NybbleBASIC\Minimize2.BMP"

'Logo image
$RESOURCE Logo AS "C:\NybbleBASIC\Logo.bmp"

'Splash image
$RESOURCE Splash AS "C:\NybbleBASIC\Splash.bmp"

'Icon
$RESOURCE MainIcon AS "C:\NybbleBASIC\Ide.Ico"
Application.IconHandle = MainIcon

'Dialog boxes
DIM Fontdlg AS QFONTDIALOG
DIM open AS QOPENDIALOG
DIM save AS QSAVEDIALOG
Fontdlg.DelOptions 2
open.filter = "Nybble BASIC files (*.nba)|*.nba|All Files (*.*)|*.*"
save.filter = "Nybble BASIC files (*.nba)|*.nba|All Files (*.*)|*.*"

'Registry
Const HKEY_LOCAL_MACHINE = &H80000002

DIM GetInfo AS QREGISTRY
GetInfo.RootKey = HKEY_LOCAL_MACHINE
GetInfo.OpenKey ("SOFTWARE\NybbleBASIC",True)

DIM Codefont AS QFONT
Codefont.name = "Courier New"
Codefont.size = 10
Codefont.color = 0

DIM Codeboxfont AS QFONT
Codeboxfont.Color = 16777215

DIM Titlefont AS QFONT
Titlefont.Color = 16777215
Titlefont.AddStyles(fsBold)

DIM TipTitle AS QFONT
TipTitle.AddStyles(fsBold)
TipTitle.Size = 14

'Subroutines
DECLARE SUB CoolBtn1Click (Sender AS QCOOLBTN)
DECLARE SUB CoolBtn2Click (Sender AS QCOOLBTN)
DECLARE SUB CoolBtn3Click (Sender AS QCOOLBTN)
DECLARE SUB CoolBtn4Click (Sender AS QCOOLBTN)
DECLARE SUB CoolBtn5Click (Sender AS QCOOLBTN)
DECLARE SUB CoolBtn6Click (Sender AS QCOOLBTN)
DECLARE SUB CoolBtn8Click (Sender AS QCOOLBTN)
DECLARE SUB Button1Click (Sender AS QBUTTON)
DECLARE SUB colPanel2Click (Sender AS QPANEL)
DECLARE SUB colButton1Click (Sender AS QBUTTON)
DECLARE SUB colButton2Click (Sender AS QBUTTON)
DECLARE SUB colPanel1Click (Sender AS QPANEL)
DECLARE SUB colScrollBar1Change (Sender AS QSCROLLBAR)
DECLARE SUB colScrollBar2Change (Sender AS QSCROLLBAR)
DECLARE SUB colScrollBar3Change (Sender AS QSCROLLBAR)
DECLARE SUB comButton1Click (Sender AS QBUTTON)
DECLARE SUB comButton2Click (Sender AS QBUTTON)
DECLARE SUB RegUserButton1Click (Sender AS QBUTTON)
DECLARE SUB OutImage2Click (Sender AS QIMAGE)
DECLARE SUB OutImage3Click (Sender AS QIMAGE)
DECLARE SUB OutImage2MouseDown (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB OutImage3MouseDown (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB OutImage2MouseUp (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB OutImage3MouseUp (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB CodeCoolBtn1Click (Sender AS QCOOLBTN)
DECLARE SUB CodeCoolBtn2Click (Sender AS QCOOLBTN)
DECLARE SUB CodeButton1Click (Sender AS QBUTTON)
DECLARE SUB InpButton1Click (Sender AS QBUTTON)
DECLARE SUB AboutButton1Click (Sender AS QBUTTON)
DECLARE SUB OutMouseDown (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB OutMouseMove (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB OutMouseUp (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
DECLARE SUB PluginButton1Click (Sender AS QBUTTON)
DECLARE SUB PluginButton2Click (Sender AS QBUTTON)
DECLARE SUB RichEdit1KeyPress (Key AS WORD, Sender AS QRICHEDIT)

'Custom subroutines
DECLARE SUB SaveAsDoc
DECLARE SUB CloseIt
DECLARE SUB PrintIt
DECLARE SUB Contents
DECLARE SUB About
DECLARE SUB Font
DECLARE SUB Online
DECLARE SUB IOShow
DECLARE SUB DebugIt
DECLARE SUB RunNow
DECLARE SUB CompileNow
DECLARE SUB AddDateTime
DECLARE SUB ShowPluginForm
DECLARE SUB ResetCursorPositionForMouse
DECLARE SUB ResetCursorPositionForKeyboard (Key AS WORD, Sender AS QRICHEDIT)

'IDE Form
CREATE Form AS QFORM
    Caption = "Nybble BASIC IDE"
    Width = 506
    Height = 390
    Center
    CREATE MainMenu AS QMAINMENU
        CREATE File AS QMENUITEM
            caption = "&File"
            CREATE New AS QMENUITEM
                caption = "&New"
                onClick = CoolBtn1Click
                ShortCut = "Ctrl+N"
            END CREATE
            CREATE OpenIt AS QMENUITEM
                caption = "&Open"
                onClick = CoolBtn2Click
                ShortCut = "Ctrl+O"
            END CREATE
            CREATE SaveIt AS QMENUITEM
                caption = "&Save"
                onClick = CoolBtn3Click
                ShortCut = "Ctrl+S"
            END CREATE
            CREATE SaveAs AS QMENUITEM
                caption = "Save &As..."
                onClick = SaveAsDoc
            END CREATE
            CREATE Div1 AS QMENUITEM
                caption = "-"
            END CREATE
            CREATE PrintItMenu AS QMENUITEM
                caption = "&Print"
                onClick = PrintIt
                ShortCut = "Ctrl+P"
            END CREATE
            CREATE Div2 AS QMENUITEM
                caption = "-"
            END CREATE
            CREATE close AS QMENUITEM
                caption = "E&xit"
                onClick = CloseIt
                ShortCut = "Alt+X"
            END CREATE
        END CREATE
        CREATE Edit AS QMENUITEM
            caption = "&Edit"
            CREATE Cut AS QMENUITEM
                caption = "C&ut"
                onClick = CoolBtn4Click
                ShortCut = "Ctrl+X"
            END CREATE
            CREATE Copy AS QMENUITEM
                caption = "C&opy"
                onClick = CoolBtn5Click
                ShortCut = "Ctrl+C"
            END CREATE
            CREATE Paste AS QMENUITEM
                caption = "&Paste"
                onClick = CoolBtn6Click
                ShortCut = "Ctrl+V"
            END CREATE
            CREATE Div3 AS QMENUITEM
                caption = "-"
            END CREATE
            CREATE DTAdd AS QMENUITEM
                caption = "&Date/Time"
                onClick = AddDateTime
            END CREATE
        END CREATE
        CREATE View AS QMENUITEM
            caption = "&View"
            CREATE Output AS QMENUITEM
                Caption = "&Debug Window"
                Checked = False
                onClick = IOShow
                ShortCut = "Ctrl+D"
            END CREATE
            CREATE Div4 AS QMENUITEM
                caption = "-"
            END CREATE
            CREATE NybbleBASICOptions AS QMENUITEM
                caption = "IDE &Options"
                CREATE SubNybbleBASICOptions1 AS QMENUITEM
                    caption = "&Program Options"
                    'onClick = ChangeNybbleBASICOptions
                    ShortCut = "Alt+O"
                END CREATE
                CREATE SubNybbleBASICOptions2 AS QMENUITEM
                    caption = "Codebox &Font"
                    onClick = Font
                    ShortCut = "Alt+F"
                END CREATE
            END CREATE
        END CREATE
        CREATE Pro AS QMENUITEM
            caption = "&Project"
            CREATE RunIt AS QMENUITEM
                caption = "&Run"
                onClick = RunNow
                ShortCut = "F5"
            END CREATE
            CREATE CompileIt AS QMENUITEM
                caption = "&Compile"
                onClick = CompileNow
                ShortCut = "F6"
            END CREATE
        END CREATE
        CREATE Plugins AS QMENUITEM
            Caption = "&Plugins"
            onClick = ShowPluginForm
        END CREATE
        CREATE Help AS QMENUITEM
            caption = "&Help"
            CREATE Con AS QMENUITEM
                caption = "&Contents"
                onClick = Contents
                ShortCut = "F1"
            END CREATE
            CREATE GoToWWW AS QMENUITEM
                caption = "&Dark X Online"
                onClick = Online
                ShortCut = "Ctrl+W"
            END CREATE
            CREATE Div5 AS QMENUITEM
                caption = "-"
            END CREATE
            CREATE AboutIt AS QMENUITEM
                caption = "&About"
                onClick = About
                ShortCut = "Ctrl+A"
            END CREATE
        END CREATE
    END CREATE
    CREATE Panel1 AS QPANEL
        Left = 0
        Top = 0
        Width = 498
        Height = 22
        BevelOuter = 0
        BevelWidth = 0
        Align = 1
        CREATE CoolBtn1 AS QCOOLBTN
            Left = 0
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "New"
            Align = 3
            OnClick = CoolBtn1Click
            BMPHandle = NewImg
        END CREATE
        CREATE CoolBtn2 AS QCOOLBTN
            Left = 23
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Open"
            Align = 3
            OnClick = CoolBtn2Click
            BMPHandle = OpenImg
        END CREATE
        CREATE CoolBtn3 AS QCOOLBTN
            Left = 46
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Save"
            Align = 3
            OnClick = CoolBtn3Click
            BMPHandle = SaveImg
        END CREATE
        CREATE CoolBtn4 AS QCOOLBTN
            Left = 71
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Cut"
            Align = 3
            OnClick = CoolBtn4Click
            BMPHandle = CutImg
        END CREATE
        CREATE CoolBtn5 AS QCOOLBTN
            Left = 94
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Copy"
            Align = 3
            OnClick = CoolBtn5Click
            BMPHandle = CopyImg
        END CREATE
        CREATE CoolBtn6 AS QCOOLBTN
            Left = 117
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Paste"
            Align = 3
            OnClick = CoolBtn6Click
            BMPHandle = PasteImg
        END CREATE
        CREATE CoolBtn7 AS QCOOLBTN
            Left = 142
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Run"
            Align = 3
            OnClick = RunNow
            BMPHandle = RunImg
        END CREATE
        CREATE CoolBtn9 AS QCOOLBTN
            Left = 165
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Compile"
            Align = 3
            OnClick = CompileNow
            BMPHandle = CompileImg
        END CREATE
        CREATE CoolBtn10 AS QCOOLBTN
            Left = 190
            Top = 0
            Width = 23
            Height = 22
            Flat = 1
            ShowHint = 1
            Hint = "Help"
            Align = 3
            OnClick = Contents
            BMPHandle = HelpImg
        END CREATE
        CREATE Image1 AS QIMAGE
            BMPHandle = Top
            Left = 298
            Top = 0
            Width = 200
            Height = 22
            Align = 4
        END CREATE
        CREATE Panel2 AS QPANEL
            Left = 188
            Top = 0
            Width = 2
            Height = 22
            Align = 3
        END CREATE
        CREATE Panel3 AS QPANEL
            Left = 69
            Top = 0
            Width = 2
            Height = 22
            Align = 3
            TabOrder = 1
        END CREATE
        CREATE Panel4 AS QPANEL
            Left = 140
            Top = 0
            Width = 2
            Height = 22
            Align = 3
            TabOrder = 2
        END CREATE
    END CREATE
    CREATE GroupBox1 AS QGROUPBOX
        Caption = "Nybble BASIC Debug"
        Left = 0
        Top = 258
        Width = 498
        Align = 2
        TabOrder = 1
        Visible = 0
        CREATE RichEdit2 AS QRICHEDIT
            BorderStyle = 0
            Left = 2
            Top = 15
            Width = 494
            Height = 88
            Align = 5
            Color = -2147483633
            HideScrollBars = 0
            PlainText = 1
            ReadOnly = 1
            ScrollBars = 2
        END CREATE
    END CREATE
    CREATE RichEdit1 AS QRICHEDIT
        Left = 0
        Top = 22
        Width = 498
        Height = 236
        Align = 5
        HideScrollBars = 0
        PlainText = 1
        ScrollBars = 3
        WordWrap = 0
        TabOrder = 2
        onKeyPress = RichEdit1KeyPress
        Font = Codefont
    END CREATE
END CREATE

'Output form
CREATE FormShow AS QFORM
    Caption = ""
    Width = 401
    Height = 240
    Center
    BorderStyle = bsSizeable
    HideTitleBar
    CREATE OutImage1 AS QIMAGE    
        BMPHandle = Title
        Left = 0
        Top = 0
        Width = 393
        Height = 15
        Align = 1
        Stretch = 1
        OnMouseDown = OutMouseDown
        OnMouseMove = OutMouseMove
        OnMouseUp = OutMouseUp
    END CREATE
    CREATE OutLabel1 AS QLABEL
        Caption = "Nybble BASIC"
        Left = 0
        Top = 1
        Width = 56
        Transparent = True
        Font = Titlefont
        OnMouseDown = OutMouseDown
        OnMouseMove = OutMouseMove
        OnMouseUp = OutMouseUp
    END CREATE
    CREATE OutImage2 AS QIMAGE
        Transparent = True
        BMPHandle = CloseImg
        Left = FormShow.Width - 25
        Top = 0
        Width = 17
        Height = 17
        OnClick = OutImage2Click
        OnMouseDown = OutImage2MouseDown
        OnMouseUp = OutImage2MouseUp
    END CREATE
    CREATE OutImage3 AS QIMAGE
        Transparent = 1
        BMPHandle = MinImg
        Left = FormShow.Width - 40
        Top = 0
        Width = 17
        Height = 17
        OnClick = OutImage3Click
        OnMouseDown = OutImage3MouseDown
        OnMouseUp = OutImage3MouseUp
    END CREATE
    CREATE RichEdit3 AS QRICHEDIT
        Left = 0
        Top = 15
        Width = 393
        Height = 198
        Align = 5
        Color = 0
        ReadOnly = 1
        Font = Codeboxfont
        onMouseUp = ResetCursorPositionForMouse
        onKeyPress = ResetCursorPositionForKeyboard
    END CREATE
END CREATE

'About dialog
CREATE AboutForm AS QFORM
    Caption = "About the Nybble BASIC IDE"
    Width = 222
    Height = 238
    BorderStyle = 4
    Center
    CREATE AboutLabel1 AS QLABEL
        Caption = "This Nybble BASIC IDE is registered to:"
        Left = 5
        Top = 116
        Transparent = 1
    END CREATE
    CREATE AboutPanel1 AS QPANEL
        Left = 5
        Top = 8
        Width = 204
        Height = 104
        BevelOuter = 1
        BevelWidth = 2
        CREATE AboutImage1 AS QIMAGE
            Left = 2
            Top = 2
            Width = 200
            Height = 100
            Align = 5
            BMPHandle = Logo
        END CREATE
    END CREATE
    CREATE AboutPanel2 AS QPANEL
        Left = 14
        Top = 131
        BevelOuter = 1
        BevelWidth = 2
        TabOrder = 1
        CREATE AboutLabel2 AS QLABEL
            Caption = "Name"
            Left = 4
            Top = 4
            Transparent = 1
        END CREATE
        CREATE AboutLabel3 AS QLABEL
            Caption = "Company"
            Left = 4
            Top = 20
            Width = 28
            Transparent = 1
        END CREATE
    END CREATE
    CREATE AboutPanel3 AS QPANEL
        Left = 1
        Top = 176
        Width = 209
        Height = 2
        TabOrder = 2
    END CREATE
    CREATE AboutButton1 AS QBUTTON
        Caption = "&Close"
        Left = 131
        Top = 182
        TabOrder = 3
        OnClick = AboutButton1Click
    END CREATE
END CREATE    

'Input form (Soon to be removed)
CREATE inpform AS QFORM
    Caption = "Input"
    Width = 320
    Height = 63
    borderstyle = 4
    Center
    CREATE inpLabel AS QLABEL
        Caption = ""
        Left = 2
        Top = 0
        Transparent = 1
    END CREATE
    CREATE inpEdit AS QEDIT
        Text = ""
        Left = 1
        Top = 13
        Width = 233
    END CREATE
    CREATE inpButton AS QBUTTON
        Caption = "&Ok"
        Left = 237
        Top = 13
        Width = 73
        Height = 21
        TabOrder = 1
        OnClick = Button1Click
    END CREATE
END CREATE

'Registry form
CREATE RegUserForm AS QFORM
    Caption = "Register this Nybble BASIC IDE"
    Width = 223
    Height = 248
    BorderStyle = 4
    Center
    CREATE RegUserLabel1 AS QLABEL
        Caption = "User Name:"
        Left = 5
        Top = 117
        Width = 56
    END CREATE
    CREATE RegUserLabel2 AS QLABEL
        Caption = "Company:"
        Left = 5
        Top = 157
        Width = 47
    END CREATE
    CREATE RegUserPanel1 AS QPANEL
        Left = 5
        Top = 8
        Width = 204
        Height = 104
        BevelOuter = 1
        BevelWidth = 2
        CREATE RegUserImage1 AS QIMAGE
            Left = 2
            Top = 2
            Width = 200
            Height = 100
            Align = 5
            BMPHandle = Logo
        END CREATE
    END CREATE
    CREATE RegUserEdit1 AS QEDIT
        Text = ""
        Left = 5
        Top = 130
        Width = 200
        TabOrder = 1
    END CREATE
    CREATE RegUserEdit2 AS QEDIT
        Text = ""
        Left = 5
        Top = 170
        Width = 200
        TabOrder = 2
    END CREATE
    CREATE RegUserButton1 AS QBUTTON
        Caption = "&OK"
        Left = 130
        Top = 193
        TabOrder = 3
        OnClick = RegUserButton1Click
    END CREATE
END CREATE

'Help form
CREATE HelpForm AS QFORM
    Caption = "Help"
    Width = 470
    Height = 334
    ICOHandle = HelpFormICOHandle
    Center
    CREATE HelpPanel1 AS QPANEL
        Left = 0
        Top = 0
        Width = 25
        Height = 307
        BevelOuter = 0
        BevelWidth = 0
        Align = 3
        Color = 0
        CREATE HelpImage1 AS QIMAGE
            BMPHandle = HelpSideImg
            Left = 0
            Top = 0
            Width = 25
            Height = 307
            Align = 3
        END CREATE
    END CREATE
    CREATE HelpRichEdit1 AS QRICHEDIT
        Left = 25
        Top = 0
        Width = 437
        Height = 307
        Align = 5
        HideScrollBars = 0
        ReadOnly = 1
        ScrollBars = 2
        TabOrder = 1
    END CREATE
END CREATE

'Splash form
CREATE SplashForm AS QFORM
    Caption = "Nybble BASIC"
    Width = 207
    Height = 341
    BorderStyle = 4
    Center
    CREATE SplashImage1 AS QIMAGE
        BMPHandle = Splash
        Left = -1
        Top = 0
        Width = 200
        Height = 250
        AutoSize = 1
    END CREATE
    CREATE SplashLabel1 AS QLABEL
        Caption = "Initialising..."
        Left = 0
        Top = 301
        Width = 199
        Align = 2
        Alignment = 2
    END CREATE
    CREATE SplashPanel1 AS QPANEL
        Left = 14
        Top = 264
        Width = 169
        Height = 25
        BevelOuter = 1
        CREATE SplashGauge1 AS QGAUGE
            Align = 5
            Left = 1
            Top = 1
            Width = 167
            Height = 23
            BorderStyle = 0
            Kind = 1
            BackColor = 12632256
            ShowText = 0
            Max = 6
        END CREATE
    END CREATE
END CREATE

'Plugin form
CREATE PluginForm AS QFORM
    Caption = "Plugins"
    Width = 228
    Height = 187
    BorderStyle = 4
    Center
    CREATE PluginLabel1 AS QLABEL
        Caption = "Plugin list:"
        Left = 1
        Top = 6
        Width = 88
        Transparent = 1
    END CREATE
    CREATE PluginListBox1 AS QLISTBOX
        Left = 0
        Top = 25
        Width = 217
    END CREATE
    CREATE PluginButton1 AS QBUTTON
        Caption = "&Run plugin"
        Left = 12
        Top = 132
        TabOrder = 1
        OnClick = PluginButton1Click
    END CREATE
    CREATE PluginButton2 AS QBUTTON
        Caption = "&Close"
        Left = 132
        Top = 132
        TabOrder = 2
        OnClick = PluginButton2Click
    END CREATE
END CREATE

SplashForm.Show
SplashForm.Repaint

SplashGauge1.Position = SplashGauge1.Position + 1

'Variables
DIM AlreadyRegVar AS INTEGER
DIM VarName(0 TO 100) AS STRING
DIM VarValue(0 TO 100) AS STRING
DIM VarTypeVal(0 TO 100) AS INTEGER

'Variable types:
'    1 = String
'    2 = Integer
'    3 = Boolean

'This is how it works:

'VarName|VarValue|VarTypeVal
'-------|--------|----------
'ABC    |Hello   |1

DIM NumVars AS INTEGER
NumVars = 0

'User defined functions
DIM FuncName(0 TO 100) AS STRING
DIM FuncLib(0 TO 100) AS STRING
DIM FuncAlias(0 TO 100) AS INTEGER

'This is how it works:

'FUNCTION a LIB "gdi.dll" ALIAS "Cool"

'FuncName|FuncLib|FuncAlias
'--------|-------|---------
'a       |gdi.dll|Cool

DIM NumFunc AS INTEGER
NumFunc = 0

SplashLabel1.Caption = "Initialising parser..."

SplashGauge1.Position = SplashGauge1.Position + 1

'Parser stuff
DIM ParseStr(0 TO 10) AS STRING
DIM Line$ AS STRING
DIM LastParse AS INTEGER
DIM LenParse AS INTEGER
DIM ParseNum AS INTEGER
DIM ReturnVar AS INTEGER
DIM IfOpen AS INTEGER
IfOpen = False
DIM EndIfFound AS INTEGER
DIM ElseFound AS INTEGER
DIM AddLine AS INTEGER
DIM RegisteredVar AS INTEGER
DIM VarNumber AS INTEGER
DIM ResumeExecution AS INTEGER
ResumeExecution = False

SplashLabel1.Caption = "Initialising interpreter..."

SplashGauge1.Position = SplashGauge1.Position + 1

'Keywords
DIM KeyWordCount AS INTEGER
KeyWordCount = 27

DIM KeyWords(1 TO KeyWordCount) AS STRING
KeyWords(1) = "CLOSE"
KeyWords(2) = "CLS"
KeyWords(3) = "DELETE"
KeyWords(4) = "IF"
KeyWords(5) = "ELSE"
KeyWords(6) = "ENDIF"
KeyWords(7) = "INPUT"
KeyWords(8) = "MAIL"
KeyWords(9) = "PRINT"
KeyWords(10) = "TITLE"
KeyWords(11) = "MKDIR"
KeyWords(12) = "RMDIR"
KeyWords(13) = "OPEN"
KeyWords(14) = "RUN"
KeyWords(15) = "MESSAGE"
KeyWords(16) = "CLOSE"
KeyWords(17) = "RESTART"
KeyWords(18) = "SLEEP"
KeyWords(19) = "FCOLOR"
KeyWords(20) = "BCOLOR"
KeyWords(21) = "DIM"
KeyWords(22) = "FUNCTION"
KeyWords(23) = "ERROR"
KeyWords(24) = "CAPTUREMOUSE"
KeyWords(25) = "SHOWCURSOR"
KeyWords(26) = "HIDECURSOR"
KeyWords(27) = "SWAPMOUSE"
'Boolean for swapmouse
DIM IsMouseSwapped AS INTEGER
IsMouseSwapped = False
'=====================

'Sub keywords
DIM SubKeyWordCount AS INTEGER
SubKeyWordCount = 7

DIM SubKeyWords(1 TO SubKeyWordCount) AS STRING
SubKeyWords(1) = "THEN"
SubKeyWords(2) = "COMPUTER"
SubKeyWords(3) = "TIME$"
SubKeyWords(4) = "DATE$"
SubKeyWords(5) = "LINEADD"
SubKeyWords(6) = "LIB"
SubKeyWords(7) = "ALIAS"

SplashLabel1.Caption = "Getting user information..."

SplashGauge1.Position = SplashGauge1.Position + 1

'If not registered IDE
if GetInfo.ReadString("UserName") = "" then RegUserForm.ShowModal

'Info in About dialog
AboutLabel2.Caption = GetInfo.ReadString("UserName")
AboutLabel3.Caption = GetInfo.ReadString("Company")

SplashLabel1.Caption = "Loading help..."

SplashGauge1.Position = SplashGauge1.Position + 1

'Help file
HelpRichEdit1.LoadFromFile ("Help.rtf")

SplashLabel1.Caption = "Loading IDE..."

SplashGauge1.Position = SplashGauge1.Position + 1

'Saving stuff
DIM Saved AS INTEGER
Saved = False
DIM SaveName AS STRING
DIM NeedToSave AS INTEGER
NeedToSave = False
DIM UserConfirm AS INTEGER

'-------------------------------------------
'New to my program - plugins!
SplashLabel1.Caption = "Loading plugins..."

SplashGauge1.Position = SplashGauge1.Position + 1

'First we dim the filestreams
DIM PluginLoader AS QFILESTREAM
DIM PluginData AS QFILESTREAM

'And now the variables
DIM PluginFiles(0 TO 500) AS STRING

PluginLoader.Open("Plugins.inc",fmOpenRead)

SplashGauge1.Position = 0
SplashGauge1.Max = PluginLoader.LineCount

for pl = 0 to PluginLoader.LineCount
    SplashGauge1.Position = SplashGauge1.Position + 1
    PluginData.Open(PluginLoader.ReadLine,fmOpenRead)
    PluginName$ = PluginData.ReadLine
    PluginFiles(pl) = PluginData.ReadLine
    SplashLabel1.Caption = "Loading " + PluginName$
    PluginListBox1.AddItems(PluginName$)
    PluginData.Close
next pl
'Now we close the filestream
PluginLoader.Close
'-------------------------------------------

SplashLabel1.Caption = "Nearly done..."

'Internal stuff
DIM MoveCodeWin AS INTEGER
MoveCodeWin = false
DIM OldX AS INTEGER
DIM OldY AS INTEGER
DIM FormX AS INTEGER
DIM FormY AS INTEGER
if command$(1) <> "" then Richedit1.LoadFromFile (command$(1))
Application.Title = "Nybble BASIC"

SplashLabel1.Caption = "Welcome to Nybble BASIC"

SplashForm.Close

Form.ShowModal

SUB PrintIt
For x = 0 to RichEdit1.LineCount - 1
    LPrint RichEdit1.Line(x)
Next x
LFlush
END SUB

SUB CoolBtn1Click (Sender AS QCOOLBTN)

if Saved = True and NeedToSave = True then
    UserConfirm = MessageDlg("Save your changes to " + SaveName + "?",mtConfirmation, mbYes OR mbNo OR mbCancel, 0)
    if UserConfirm = 6 then
        if right$(SaveName,4) = ".nba" then richedit1.savetofile (SaveName) else richedit1.savetofile (SaveName + ".nba")
        RichEdit1.clear
        Saved = False
        SaveName = ""
        save.filename = ""
        open.filename = ""
        NeedToSave = False
    elseif UserConfirm = 7 then
        RichEdit1.clear
        Saved = False
        SaveName = ""
        save.filename = ""
        open.filename = ""
        NeedToSave = False
    end if
elseif Saved = False and NeedToSave = True then
    UserConfirm = MessageDlg("Save your changes to Untitled?",mtConfirmation, mbYes OR mbNo OR mbCancel, 0)
    if UserConfirm = 6 then
        if save.execute and save.filename <> "" then
            if right$(save.filename,4) = ".nba" then richedit1.savetofile (save.filename) else richedit1.savetofile (save.filename + ".nba")
        end if
        RichEdit1.clear
        Saved = False
        SaveName = ""
        save.filename = ""
        open.filename = ""
        NeedToSave = False
    elseif UserConfirm = 7 then
        RichEdit1.clear
        Saved = False
        SaveName = ""
        save.filename = ""
        open.filename = ""
        NeedToSave = False
    end if
else
    RichEdit1.clear
    Saved = False
    SaveName = ""
    save.filename = ""
    open.filename = ""
    NeedToSave = False
end if
END SUB

SUB CoolBtn2Click (Sender AS QCOOLBTN)
if Saved = True and NeedToSave = True then
    UserConfirm = MessageDlg("Save your changes to " + SaveName + "?",mtConfirmation, mbYes OR mbNo OR mbCancel, 0)
    if UserConfirm = 6 then
        if right$(SaveName,4) = ".nba" then richedit1.savetofile (SaveName) else richedit1.savetofile (SaveName + ".nba")
        if open.execute and open.filename <> "" then
            richedit1.loadfromfile (open.filename)
            Saved = True
            SaveName = open.filename
            save.filename = ""
            open.filename = ""
            NeedToSave = False
        end if
    elseif UserConfirm = 7 then
        if open.execute and open.filename <> "" then
            richedit1.loadfromfile (open.filename)
            Saved = True
            SaveName = open.filename
            save.filename = ""
            open.filename = ""
            NeedToSave = False
        end if
    end if
elseif Saved = False and NeedToSave = True then
    UserConfirm = MessageDlg("Save your changes to Untitled?",mtConfirmation, mbYes OR mbNo OR mbCancel, 0)
    if UserConfirm = 6 then
        if save.execute and save.filename <> "" then
            if right$(save.filename,4) = ".nba" then richedit1.savetofile (save.filename) else richedit1.savetofile (save.filename + ".nba")
        end if
        if open.execute and open.filename <> "" then
            richedit1.loadfromfile (open.filename)
            Saved = True
            SaveName = open.filename
            save.filename = ""
            open.filename = ""
            NeedToSave = False
        end if
    elseif UserConfirm = 7 then
        if open.execute and open.filename <> "" then
            richedit1.loadfromfile (open.filename)
            Saved = True
            SaveName = open.filename
            save.filename = ""
            open.filename = ""
            NeedToSave = False
        end if
    end if
else
    if open.execute and open.filename <> "" then
        richedit1.loadfromfile (open.filename)
        Saved = True
        SaveName = open.filename
        save.filename = ""
        open.filename = ""
        NeedToSave = False
    end if
end if
END SUB

SUB CoolBtn3Click (Sender AS QCOOLBTN)
if Saved = False then
    SaveAsDoc
else
    if right$(SaveName,4) = ".nba" then richedit1.savetofile (SaveName) else richedit1.savetofile (SaveName + ".nba")
    NeedToSave = False
end if
END SUB

SUB CoolBtn4Click (Sender AS QCOOLBTN)
if richedit1.seltext <> "" then richedit1.cuttoclipboard else showmessage ("You must select text to be cut!")
END SUB

SUB CoolBtn5Click (Sender AS QCOOLBTN)
if richedit1.seltext <> "" then richedit1.copytoclipboard else showmessage ("You must select text to be copied!")
END SUB

SUB CoolBtn6Click (Sender AS QCOOLBTN)
richedit1.pastefromclipboard
END SUB

SUB CloseIt
if Saved = True and NeedToSave = True then
    UserConfirm = MessageDlg("Save your changes to " + SaveName + "?",mtConfirmation, mbYes OR mbNo OR mbCancel, 0)
    if UserConfirm = 6 then
        if right$(SaveName,4) = ".nba" then richedit1.savetofile (SaveName) else richedit1.savetofile (SaveName + ".nba")
        Form.Close
    elseif UserConfirm = 7 then
        Form.Close
    end if
elseif Saved = False and NeedToSave = True then
    UserConfirm = MessageDlg("Save your changes to Untitled?",mtConfirmation, mbYes OR mbNo OR mbCancel, 0)
    if UserConfirm = 6 then
        if save.execute and save.filename <> "" then
            if right$(save.filename,4) = ".nba" then richedit1.savetofile (save.filename) else richedit1.savetofile (save.filename + ".nba")
        end if
        Form.Close
    elseif UserConfirm = 7 then
        Form.Close
    end if
else
    Form.Close
end if
END SUB

SUB Contents
HelpForm.Show
END SUB

SUB About
HelpRichEdit1.SelStart = 0
AboutForm.ShowModal
END SUB

SUB Font
fontdlg.setfont (Codefont)
if fontdlg.execute then
Codefont.size = fontdlg.size
Codefont.name = fontdlg.name
richedit1.font = Codefont
end if
END SUB

SUB Online
run("Explorer.exe http://www.freewebs.com/darkxsoftware")
END SUB

SUB SaveAsDoc
if save.execute and save.filename <> "" then
    if right$(save.filename,4) = ".nba" then richedit1.savetofile (save.filename) else richedit1.savetofile (save.filename + ".nba")
    SaveName = save.filename
    Saved = True
    save.filename = ""
    open.filename = ""
    NeedToSave = False
end if
END SUB

SUB DebugIt

if Output.Checked = false then Output.Checked = true

GroupBox1.visible = true
richedit2.clear

if richedit1.linecount = 0 then
    richedit2.addstring "No Code!"
end if

END SUB

$ESCAPECHARS ON

SUB RunNow
DebugIt

Richedit3.Clear
FormShow.Show


'Clear out the variables + reset the amount of variables to 0
for ClearVar = 0 to NumVars
    VarName(ClearVar) = ""
    VarValue(ClearVar) = ""
    VarTypeVal(ClearVar) = 0
next ClearVar

NumVars = 0

'New + improved method - parsing!

11
for line = 0 to Richedit1.LineCount - 1

'========================================
'Parse engine - (c) Ben Sherratt 2002
'========================================

'Clean out the last set of parsed strings
for ClearParse = 0 to ParseNum
    ParseStr(ClearParse) = ""
next ClearParse

Line$ = LTrim$(RTrim$(RichEdit1.Line(Line)))
LastParse = 0
LenParse = 0
ParseNum = 0
Speech = False
for ParseIt = 0 to len(Line$)
    'Speech marks not to be parsed
    if mid$(Line$,ParseIt,1) = "\"" then
        if Speech = False then Speech = True else Speech = False
    end if
    'This is to see if to break up the string
    if (mid$(Line$,ParseIt,1) = " " and Speech = False) or (mid$(Line$,ParseIt,1) = "," and Speech = False) or (mid$(Line$,ParseIt,1) = "(" and Speech = False) or (mid$(Line$,ParseIt,1) = ")" and Speech = False) then
        'This is to check that it is not a blank parse
        if mid$(Line$,LastParse,LenParse - 1) = "" then
            'Make sure that we add one to the position of the last parse
            'This means that the blank space is skipped
            LastParse = ParseIt + 1
            'Make sure that the length of the parse is reset
            LenParse = 0
        else
            ParseStr(ParseNum) = (mid$(Line$,LastParse,LenParse - 1))
            'Position of last parse
            LastParse = ParseIt + 1
            'Length of parse reset
            LenParse = 0
            'Number of parsed strings goes up by one
            ParseNum = ParseNum + 1
        end if
    end if
    'Add to parse length
    LenParse = LenParse + 1
next ParseIt

'Last string, has no parse identifier so must just be added if there is not a closing bracket
if right$(Line$,1) <> ")" then ParseStr(ParseNum) = (mid$(Line$,LastParse,LenParse - 1))

'========================================
'End of Parse engine
'========================================

'First, we test the keyword
RegKeyWord = false

for KeyCheck = 1 to KeyWordCount
    if KeyWords(KeyCheck) = ucase$(ParseStr(0)) then
        RegKeyWord = true
        exit for
    end if
next KeyCheck

' ' is used to add comments to the code
if RegKeyWord = False and Ltrim$(Left$(Line$,1)) <> "'" and LTrim$(Line$) <> "" then
    'What to do if the command is not recognised
    'Check for registered var
    
    RegisteredVar = False
    
    for VarCnt = 0 to NumVars
        if VarName(VarCnt) = ucase$(ParseStr(0)) then
            RegisteredVar = True
            VarNumber = VarCnt
            Exit For
        end if
    next VarCnt
    
    for FuncCnt = 0 to NumFunc
        if FuncName(FuncCnt) = ucase$(ParseStr(0)) then
            RegisteredFunc = True
            FuncNumber = FuncCnt
            Exit For
        end if
    next FuncCnt
    
    if RegisteredVar = True then
        if ParseStr(1) = "=" then
            if VarTypeVal(VarCnt) = 1 then
                if left$(ParseStr(2),1) = "\"" and right$(ParseStr(2),1) = "\"" then
                    VarValue(VarCnt) = mid$(ParseStr(2),2,len(ParseStr(2)) - 2)
                else
                    richedit2.addstring ("Error on line " + str$(line + 1) + "- invalid data, expected a string")
                    exit for
                end if
            elseif VarTypeVal(VarCnt) = 2 then
                if left$(ParseStr(2),1) <> "\"" and right$(ParseStr(2),1) <> "\"" then
                    VarValue(VarCnt) = ParseStr(2)
                else
                    richedit2.addstring ("Error on line " + str$(line + 1) + "- invalid data, expected an integer")
                    exit for
                end if
            elseif VarTypeVal(VarCnt) = 3 then
                if ucase$(ParseStr(2)) = "TRUE" or ucase$(ParseStr(2)) = "FALSE" then
                    if ucase$(ParseStr(2)) = "TRUE" then VarValue(VarCnt) = "T" else VarValue(VarCnt) = "F"
                else
                    richedit2.addstring ("Error on line " + str$(line + 1) + "- invalid data, expected TRUE or FALSE")
                    exit for
                end if
            end if
        elseif RegisteredFunc = True then
            'Function stuff here
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- invalid operator " + ParseStr(1))
            exit for
        end if
    else
        richedit2.addstring ("Error on line " + str$(line + 1) + "- unrecognised command " + ParseStr(0))
        exit for
    end if
else
    'What to do if it is recognised

    if ucase$(ParseStr(0)) = "SWAPMOUSE" then
        if IsMouseSwapped = False then
            IsMouseSwapped = True
            SwapMouseButton(True)
        else
            SwapMouseButton(False)
            IsMouseSwapped = False
        end if
    end if

    if ucase$(ParseStr(0)) = "SHOWCURSOR" then
        ShowCursor (true)
    end if
        
    if ucase$(ParseStr(0)) = "HIDECURSOR" then
        ShowCursor (false)
    end if
    
    if ucase$(ParseStr(0)) = "CAPTUREMOUSE" then
        WinRect.Top = val(ParseStr(1))
        WinRect.Bottom = val(ParseStr(2))
        WinRect.Left = val(ParseStr(3))
        WinRect.Right = val(ParseStr(4))
        ClipCursor(WinRect)
    end if
    
    if ucase$(ParseStr(0)) = "ERROR" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            FatalAppExit(0,inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                FatalAppExit(0,ParseStr(1))
            else
                richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if
    
    if ucase$(ParseStr(0)) = "FUNCTION" then
        if ucase$(ParseStr(2)) = "LIB" and ucase$(ParseStr(4)) = "ALIAS" then
            
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- incorrect definition of function")
            exit for
        end if
    end if
    
    if ucase$(ParseStr(0)) = "DIM" then
        'Check for the AS
        if ucase$(ParseStr(2)) = "AS" then
            AlreadyRegVar = False
            
            for VarCnt = 0 to NumVars
                if VarName(VarCnt) = ucase$(ParseStr(1)) then
                    AlreadyRegVar = True
                    Exit For
                end if
            next VarCnt
             
            for VarCnt = 0 to KeyWordCount
                if KeyWords(VarCnt) = ucase$(ParseStr(1)) then
                    AlreadyRegVar = True
                    Exit For
                end if
            next VarCnt
            
             
            for VarCnt = 0 to SubKeyWordCount
                if SubKeyWords(VarCnt) = ucase$(ParseStr(1)) then
                    AlreadyRegVar = True
                    Exit For
                end if
            next VarCnt
            
            if AlreadyRegVar = False then
                'Now we get the type of variable and enter data to reflect this
                if ucase$(ParseStr(3)) = "STRING" then
                    'Enter the name
                    VarName(NumVars) = ucase$(ParseStr(1))
                    'Enter a NULL value
                    VarValue(NumVars) = ""
                    'Set the type
                    VarTypeVal(NumVars) = 1
                    'Add one to the number of variables
                    NumVars = NumVars + 1
                elseif ucase$(ParseStr(3)) = "INTEGER" then
                    'Enter the name
                    VarName(NumVars) = ucase$(ParseStr(1))
                    'Enter a NULL value
                    VarValue(NumVars) = ""
                    'Set the type
                    VarTypeVal(NumVars) = 2
                    'Add one to the number of variables
                    NumVars = NumVars + 1
                elseif ucase$(ParseStr(3)) = "BOOLEAN" then
                    'Enter the name
                    VarName(NumVars) = ucase$(ParseStr(1))
                    'Enter a NULL value
                    VarValue(NumVars) = ""
                    'Set the type
                    VarTypeVal(NumVars) = 3
                    'Add one to the number of variables
                    NumVars = NumVars + 1
                else
                    richedit2.addstring ("Error on line " + str$(line + 1) + "- unrecognised variable type " + ParseStr(3))
                    exit for
                end if
            else
                richedit2.addstring ("Error on line " + str$(line + 1) + "- already registered variable or keyword \"" + ParseStr(1) + "\"")
                exit for
            end if
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- no AS")
            exit for
        end if
    end if
    
    
    if ucase$(ParseStr(0)) = "ELSE" then
        if IfOpen = True then
            IfOpen = False
            AddLine = 0
            for EndIfLineCount = Line to RichEdit1.LineCount
                AddLine = AddLine + 1
                if ltrim$(rtrim$(ucase$(RichEdit1.Line(EndIfLineCount)))) = "ENDIF" then
                    'This is to move to the close of the If statement
                    Line = Line + AddLine - 1 'This moves it to the right line
                    exit for
                end if
            next EndIfLineCount
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- no IF statement for this ELSE")
            exit for
        end if
    end if
    
    if ucase$(ParseStr(0)) = "ENDIF" then
        if IfOpen = True then
            IfOpen = False
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- no IF statement for this ENDIF")
            exit for
        end if
    end if
    
    if ucase$(ParseStr(0)) = "IF" then
        if ucase$(ParseStr(4)) = "THEN" then
            EndIfFound = False
            for EndIfLineCount = Line to RichEdit1.LineCount
                if ltrim$(rtrim$(ucase$(RichEdit1.Line(EndIfLineCount)))) = "ENDIF" then
                    'This is to make sure there is an EndIf statement
                    EndIfFound = True
                    exit for
                end if
            next EndIfLineCount
            if EndIfFound = False then
                richedit2.addstring ("Mising an ENDIF")
                exit for
            end if
            if ParseStr(2) = "=" then
                if ucase$(ParseStr(1)) = "INPUT$" then
                    if ucase$(ParseStr(3)) = "INPUT$" then
                        ReturnVar = True
                    else
                        if left$(ParseStr(3),1) = "\"" and right$(ParseStr(3),1) = "\"" then
                            ParseStr(3) = ParseStr(3) - "\""
                            if inp$ = ParseStr(3) then ReturnVar = true else ReturnVar = False
                        else
                            richedit2.addstring ("Error on line " + str$(line + 1) + "- incompatable types, input$ is a string")
                            exit for
                        end if
                    end if
                else
                    'this is where variables will go 
                end if
            elseif ParseStr(2) = "<>" then
                if ucase$(ParseStr(1)) = "INPUT$" then
                        if ucase$(ParseStr(3)) = "INPUT$" then
                            ReturnVar = False
                        else
                            if left$(ParseStr(3),1) = "\"" and right$(ParseStr(3),1) = "\"" then
                                ParseStr(3) = ParseStr(3) - "\""
                                if inp$ <> ParseStr(3) then ReturnVar = true else ReturnVar = False
                            else
                                richedit2.addstring ("Error on line " + str$(line + 1) + "- incompatable types, input$ is a string")
                                exit for
                            end if
                        end if
                    else
                        'this is where variables will go 
                    end if
                else
                richedit2.addstring ("Error on line " + str$(line + 1) + "- invalid operator " + ParseStr(2))
                exit for
            end if
        
            if ReturnVar = True then
                IfOpen = True
            elseif ReturnVar = False then
                IfOpen = True
                ElseFound = False
                AddLine = 0
                for ElseLineCount = Line to RichEdit1.LineCount
                    AddLine = AddLine + 1
                    if ltrim$(rtrim$(ucase$(RichEdit1.Line(ElseLineCount)))) = "ELSE" then
                        'This is what will happen if an else is found in the code
                        Line = Line + AddLine - 1 'This moves it to the right line
                        ElseFound = True
                        exit for
                    end if
                next ElseLineCount

                if ElseFound = False then
                    AddLine = 0
                    for EndIfLineCount = Line to RichEdit1.LineCount
                    AddLine = AddLine + 1
                        if ltrim$(rtrim$(ucase$(RichEdit1.Line(EndIfLineCount)))) = "ENDIF" then
                            'This is to move to the close of the If statement
                            Line = Line + AddLine - 1
                            IfOpen = False
                            exit for
                        end if
                    next EndIfLineCount
                end if
            end if
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- missing the command THEN")
            exit for
        end if
    end if
    
    if ucase$(ParseStr(0)) = "FCOLOR" then
        if ParseStr(1) <> "" and left$(ParseStr(1),1) <> "\"" and right$(ParseStr(1),1) <> "\"" then
            Codeboxfont.Color = val(ParseStr(1))
            RichEdit3.Font = Codeboxfont
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a integer")
            exit for
        end if
    end if
    
    if ucase$(ParseStr(0)) = "BCOLOR" then
        if ParseStr(1) <> "" and left$(ParseStr(1),1) <> "\"" and right$(ParseStr(1),1) <> "\"" then
            RichEdit3.Color =  val(ParseStr(1))
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a integer")
            exit for
        end if
    end if

    if ucase$(ParseStr(0)) = "SLEEP" then
        if ParseStr(1) <> "" and left$(ParseStr(1),1) <> "\"" and right$(ParseStr(1),1) <> "\"" then
            SleepEx(val(ParseStr(1)), False)
            FormShow.Repaint
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a integer")
            exit for
        end if
    end if
    
    if ucase$(ParseStr(0)) = "INPUT" then
        if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
            ParseStr(1) = ParseStr(1) - "\""
            RichEdit3.Text = RichEdit3.Text + (ParseStr(1) + " ")
            RichEdit3.SelStart = len(RichEdit3.Text)
            RichEdit3.ReadOnly = False
            While ResumeExecution = False
                DoEvents
            Wend
            RichEdit3.ReadOnly = True
            ResumeExecution = False
            inp$ = mid$(RichEdit3.Line(RichEdit3.LineCount - 1),len(ParseStr(1) + " ") + 1,Len(RichEdit3.Line(RichEdit3.LineCount - 1)))
        else
            richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a string")
            exit for
        end if
    end if

    if ucase$(ParseStr(0)) = "MAIL" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            run("rundll32.exe url.dll,MailToProtocolHandler " + inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                run("rundll32.exe url.dll,MailToProtocolHandler " + ParseStr(1))
            else
                richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "PRINT" then
        if ucase$(ParseStr(1)) = "TIME$" then
            richedit3.text = richedit3.text + (time$)
        elseif ucase$(ParseStr(1)) = "DATE$" then
            richedit3.text = richedit3.text + (date$)
        elseif ucase$(ParseStr(1)) = "ADDLINE" then
            richedit3.AddString("")
        elseif ucase$(ParseStr(1)) = "INPUT$" then
            richedit3.text = richedit3.text + (inp$)
        elseif left$(ParseStr(1),1) = "\"" or right$(ParseStr(1),1) = "\"" then
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                richedit3.text = richedit3.text + (mid$(ParseStr(1),2,len(ParseStr(1)) - 2))
            else
                richedit2.addstring("Error on line " + str$(line + 1) + " invalid string")
                exit for
            end if
        else
            RegisteredVar = False
            for VarCnt = 0 to NumVars
                if VarName(VarCnt) = ucase$(ParseStr(1)) then
                    RegisteredVar = True
                    VarNumber = VarCnt
                    Exit For
                end if
            next VarCnt
            if RegisteredVar = True then
                if VarTypeVal(VarNumber) = 1 then
                    richedit3.text = richedit3.text + (VarValue(VarNumber))
                else
                    richedit2.addstring("Error on line " + str$(line + 1) + " this variable is not a string")
                    exit for
                end if
            else
                richedit2.addstring("Error on line " + str$(line + 1) + " no string or variable")
                exit for
            end if
        end if
        richedit3.selstart = len(richedit3.text)
    end if

    if ucase$(ParseStr(0)) = "TITLE" and ParseStr(1) = "=" then
        if ucase$(ParseStr(2)) = "INPUT$" then
            application.title = inp$
            OutLabel1.Caption = inp$
        else
            if left$(ParseStr(2),1) = "\"" and right$(ParseStr(2),1) = "\"" then
                ParseStr(2) = ParseStr(2) - "\""
                application.title = ParseStr(2)
                OutLabel1.Caption = ParseStr(2)
            else
                richedit2.addstring("Error on line " + str$(line + 1))
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "MKDIR" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            mkdir(inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                mkdir(ParseStr(1))
            else
                richedit2.addstring("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "RMDIR" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            rmdir(inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                rmdir(ParseStr(1))
            else
                richedit2.addstring("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "DELETE" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            kill(inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                kill(ParseStr(1))
            else
                richedit2.addstring("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "OPEN" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            run("explorer.exe" + inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                shell("explorer.exe" + ParseStr(1))
            else
                richedit2.addstring("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "RUN" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            run(inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                shell(ParseStr(1))
            else
                richedit2.addstring("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "PLAY" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            playwav(inp$,1)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                playwav(ParseStr(1),1)
            else
                richedit2.addstring("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "MESSAGE" then
        if ucase$(ParseStr(1)) = "INPUT$" then
            showmessage(inp$)
        else
            if left$(ParseStr(1),1) = "\"" and right$(ParseStr(1),1) = "\"" then
                ParseStr(1) = ParseStr(1) - "\""
                showmessage ParseStr(1)
            else
                richedit2.addstring ("Error on line " + str$(line + 1) + "- Expected a string")
                exit for
            end if
        end if
    end if

    if ucase$(ParseStr(0)) = "CLOSE" then
        exit for
    end if

    if ucase$(ParseStr(0)) = "RESTART" then
        if ucase$(ParseStr(1)) = "" then
            goto 11
        else
            if ucase$(ParseStr(1)) = "COMPUTER" then
                out &h64, &hfe
            else
                richedit2.addstring ("Error on line " + str$(line + 1) + "- Unrecognised command " + ParseStr(1))
                exit for
            end if
        end if
    end if
    
    if ucase$(ParseStr(0)) = "CLS" then richedit3.clear
end if
'Goto next line and start again...
next line
application.title = "Nybble BASIC"

if IfOpen = True then
    richedit2.addstring ("You are missing ENDIF commands")
end if

IfOpen = False

END SUB

$ESCAPECHARS OFF

SUB CompileNow
'Code to compile
END SUB

SUB Button1Click (Sender AS QBUTTON)
inpform.close
END SUB

SUB OutImage2Click (Sender AS QIMAGE)
FormShow.close
END SUB

SUB OutImage3Click (Sender AS QIMAGE)
application.minimize
END SUB

SUB AboutButton1Click (Sender AS QBUTTON)
AboutForm.close
END SUB

SUB RegUserButton1Click (Sender AS QBUTTON)
if RegUserEdit1.text <> "" then
    GetInfo.WriteString("UserName", RegUserEdit1.Text)
    GetInfo.WriteString("Company", RegUserEdit2.Text)
    RegUserForm.Close
else
    ShowMessage ("You need to enter a user name")
end if
END SUB

SUB OutMouseDown (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
MoveCodeWin = true
OldX = X
OldY = y
FormX = FormShow.Left
FormY = FormShow.Top
END SUB

SUB OutMouseMove (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
if MoveCodeWin = true then
    FormShow.Left = FormX + (X - OldX)
    FormShow.Top = FormY + (Y - OldY)
    FormX = FormShow.Left
    FormY = FormShow.Top
end if
END SUB

SUB OutMouseUp (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
MoveCodeWin = false
END SUB

SUB OutImage2MouseDown (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
OutImage2.BMPHandle = CloseImg2
END SUB

SUB OutImage2MouseUp (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
OutImage2.BMPHandle = CloseImg
END SUB

SUB OutImage3MouseDown (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
OutImage3.BMPHandle = MinImg2
END SUB

SUB OutImage3MouseUp (X AS LONG, Y AS LONG, Shift AS LONG, Sender AS QIMAGE)
OutImage3.BMPHandle = MinImg
END SUB

SUB IOShow
if Output.Checked = false then Output.Checked = true else Output.Checked = false
if GroupBox1.Visible = false then GroupBox1.visible = true else GroupBox1.Visible = false
END SUB

SUB AddDateTime
Richedit1.AddString (date$ + "/" + time$)
END SUB

SUB PluginButton1Click (Sender AS QBUTTON)
if PluginListBox.ItemIndex > -1 then
    run ("plugins\" + PluginFiles(PluginListBox1.ItemIndex))
end if
END SUB

SUB PluginButton2Click (Sender AS QBUTTON)
PluginForm.Close
END SUB

SUB ShowPluginForm
PluginForm.ShowModal
END SUB

SUB RichEdit1KeyPress (Key AS WORD, Sender AS QRICHEDIT)
if NeedToSave = False then NeedToSave = True
END SUB

SUB ResetCursorPositionForKeyboard (Key AS WORD, Sender AS QRICHEDIT)
If RichEdit3.ReadOnly = False then
    RichEdit3.SelStart = len(RichEdit3.Text)
    if Key = 13 then ResumeExecution = True
End If
END SUB

SUB ResetCursorPositionForMouse
If RichEdit3.ReadOnly = False then
    RichEdit3.SelStart = len(RichEdit3.Text)
End If
END SUB